<template>
  <div class="warnings">
    <div class="header">
      <i class="prefix"></i>
      <span>系统告警信息</span>
      <i class="surfix"></i>
    </div>
    <div class="thead item">
      <div class="name">设备名称</div>
      <div class="level">告警级别</div>
      <div class="val">实测值</div>
      <div class="warning">告警阈值</div>
    </div>
    <div class="list">
      <div class="item pointer" v-for="(item,index) of list" :key="index" @click="clickItem(item)" title="点击定位到该传感器">
        <dot class="dot" :color="getColor(item)"></dot>
        <div class="rt">
          <div>
            <div class="name">{{item.name}}</div>
            <div class="level" :style="'color:'+getColor(item)">{{getLevel(item)}}</div>
            <div class="val" :style="'color:'+getColor(item)">{{item.val}}</div>
            <div class="warning" :style="'color:'+getColor(item)">{{item.warning}}</div>
            <i class="icon" :class="{active:1}"></i>
          </div>
          <div>
            <div class="content">{{item.content}}</div>
            <div class="time">{{item.time}}</div>
          </div>
        </div>
      </div>
      <div class="alarm-empty-value" v-if="list.length == 0">
        无数据
      </div>
    </div>
    <div class="ft">
      <span class="tomore" @click="moreAlarm(list)">查看更多<i class="el-icon-d-arrow-right el-icon--right pointer"></i>
      </span>
    </div>
  </div>

</template>

<script>

  import dot from "./dot";
  import { indexListAlarm } from "@/api/slope/alarm";
  import {popupPanel} from "./popupPanel";


  export default {
    name: "warnings",
    components: {
      dot,
      popupPanel
    },
    props: {},
    data() {
      return {
        list: []
      };
    },
    watch: {},
    computed: {},
    methods: {
      getColor({
        level
      }) {
        let ret = "";
        switch (level) {
          case 1:
            ret = "#3492FC";
            break;
          case 2:
            ret = "#FF8000";
            break;
          case 3:
            ret = "#FF0000";
            break;

          default:
            break;
        }
        return ret;
      },
      getLevel({
        level
      }) {
        let ret = "";
        switch (level) {
          case 1:
            ret = "一级";
            break;
          case 2:
            ret = "二级";
            break;
          case 3:
            ret = "三级";
            break;
          default:
            break;
        }
        return ret;
      },
      async getData() {
        let self = this;
        self.loading = true;
        indexListAlarm({
            pageNum: 1,
            pageSize: 5,
          }).then(response => {
            let data = response.rows;
            if(data.length !== 0){
              let tableData = [];
              for (let i = 0; i < data.length; i++) {
                let item = data[i];
                tableData.push({
                  level: item.alarmLevel,
                  name: item.alarmSensor.device.deviceName,
                  content: item.alarmSensor.monitorDetail,
                  val: item.alarmValue,
                  warning: item.alarmThreadhold,
                  time: this.handleTimeFormat(item.alarmTime),
                  sensor: item.alarmSensor,
                  alarm: item,
                  sensorId:item.alarmSensorId
                });
              }
              self.list = tableData;
              self.loading = false;
            }
          })
          .catch(err => {
            self.loading = false;
          });
      },
      moreAlarm(e) {
        this.$router.push({
          path: "/alarm/alarm-alarm",
          query: {
            id: 1
          }
        });

      },
      clickItem(item) {
        let coordinate = [];
        coordinate.push(item.sensor.device.point.lon);
        coordinate.push(item.sensor.device.point.lat);
        this.moveTo(coordinate);
        this.$parent.$parent.showPopup(item.sensorId)
        // this.$parent.$parent.warnItemClick(item);
      },
      handleTimeFormat(val) {
        return val.substring(0, 10) + " " + val.substring(11, 19);
      },
      moveTo(loc) {
        var view = this.$parent.$parent.olMap.getView();
        return new Promise((resolve, reject) => {
          view.animate({
              center: loc,
              zoom: 18
            },
            () => {
              resolve(view)
            }
          )
        })
      },
    },
    created() {
      this.getData();
    },
    mounted() {}
  };
</script>
<style lang="scss" scoped>
  @import "~@/assets/styles/mixin.scss";
  @import "~@/assets/styles/variables.scss";

  .warnings {
    background-color: #fff;
    border: 1px solid #eee;
    margin-top: $dsMain;
    padding: 5px;
    border-radius: 4px;
    flex-shrink: 1 !important;
    display: flex;
    flex-direction: column;

    .header {
      text-align: center;
      font-size: 14px;
      line-height: 40px;
      color: #323232;
      flex-shrink: 0;
      @include flex(center, center);

      i {
        display: block;
        width: 78px;
        height: 5px;
        background-repeat: no-repeat;
        background-size: contain;
        margin: 0 5px;

        &.prefix {
          background-image: url("~@/assets/image/icons/a-l.png");
        }

        &.surfix {
          background-image: url("~@/assets/image/icons/a-r.png");
        }
      }
    }

    .list {
      font-size: 14px;
      min-height: 0;
      padding: 0 4px;
      overflow-y: auto;
    }

    .thead {
      font-size: 12px;
      padding: 0 27px 0 20px !important;
      flex-wrap: nowrap !important;
      font-weight: bold;
    }

    .alarm-empty-value {
      color: #909399;
      text-align: center;
      height: 40px;
      line-height: 40px !important;
    }

    .item {
      border-bottom: 1px solid #eee;
      @include flex(space-between, flex-start);
      line-height: 1.7;
      flex-wrap: wrap;
      padding: 4px 0;

      .dot {
        margin-top: 5px;
      }

      .rt {
        width: calc(100% - 16px);

        >div {
          @include flex(space-between, center);
        }

        font-size: 12px;
      }

      .name {
        @include ellipsis;
        @include shrink;
        width: 80px;
      }

      .content {
        @include ellipsis;
        @include grow;
      }

      .level {
        @include ellipsis;
        width: 58px;
        margin: 0 5px;
        text-align: center;
      }

      .val {
        @include ellipsis;
        width: 58px;
        margin: 0 5px;
        text-align: center;
      }

      .warning {
        @include ellipsis;
        margin: 0 5px;
        width: 58px;
        text-align: center;
      }

      .time {
        @include shrink;
        color: #b9b9b9;
      }

      .icon {
        @include shrink;
        display: block;
        width: 18px;
        height: 18px;
        margin-left: 5px;
        background-size: contain;
        background-repeat: no-repeat;

        &.active {
          background-image: url("~@/assets/image/icons/warning.png");
        }
      }
    }

    .ft {
      border-top: 1px solid #eee;
      text-align: center;
      font-size: 14px;
      line-height: 2.5;
    }
  }
</style>
